<template>
  <div class="passenger">
    <a-card title="乘客人管理">
      <a-form layout="inline" :model="formState">
        <a-form-item>
          <a-input v-model:value="formObj.name" placeholder="请输入乘客人姓名" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="getList">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="add">新增</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table :data-source="passengerList" :columns="columns" :pagination="false">
      <template #action="{ record }">
        <a-button type="link" size="small" @click="edit(record)">编辑</a-button>
        &nbsp;&nbsp;&nbsp;
        <a-popconfirm title="确认删除吗?" @confirm="deletePassenger(record)">
          <a-button type="link" danger size="small">删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @show-size-change="onShowSizeChange"
    />
    <a-modal
      v-model:open="dialog"
      title="新增乘客"
      width="700px"
      ok-text="确认"
      cancel-text="取消"
      center
      @ok="handleOk"
      @cancel="cancel"
    >
      <a-form :model="formState">
        <a-form-item label="乘客人姓名">
          <a-input v-model:value="formState.name" placeholder="请输入乘客人姓名" />
        </a-form-item>
        <a-form-item label="乘客人身份证">
          <a-input v-model:value="formState.idCard" placeholder="请输入乘客人身份证" />
        </a-form-item>
        <a-form-item label="乘客人类型">
          <a-select v-model:value="formState.type" allow-clear>
            <a-select-option
              v-for="(item, index) in passengerTypeOptions"
              :key="index"
              :value="item.code"
            >
              {{ item.description }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref } from 'vue';
import { passengerApi } from '../../api/passenger';
import { message } from 'ant-design-vue';
import { enumApi } from '@/api/enum';
const formState = ref<any>({
  id: '',
  name: '',
  idCard: '',
  type: '',
});
const dialog = ref(false);

const columns = ref([
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
  },
  {
    title: '身份证号',
    dataIndex: 'idCard',
    key: 'idCard',
  },
  {
    title: '会员id',
    dataIndex: 'memberId',
    key: 'memberId',
  },
  {
    title: '类型',
    colSpan: 1,
    dataIndex: 'type',
    customRender: (row: any): string => {
      return getDescription(row.text);
    },
  },
  {
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
    width: 200,
    slots: { customRender: 'action' }, // 使用 slots 而不是 render 函数
  },
]);
const formObj = ref({
  name: '',
  page: 1,
  pageSize: 10,
  total: 0,
});
onMounted(() => {
  getList();
});
const passengerList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await passengerApi.queryPassenger(formObj.value);
  passengerList.value = data.records || [];
  formObj.value.total = data.total;
};
const reset = (): void => {
  formObj.value.name = '';
  getList();
};
const onShowSizeChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};
// 字典表的查询
const passengerTypeOptions = ref<any[]>([]);
// 获取枚举数据
onMounted(() => {
  loadEnumData();
});
const loadEnumData = async (): Promise<void> => {
  const { data } = await enumApi.getEnumList('passenger_type');
  passengerTypeOptions.value = data;
  console.log('passengerTypeOptions.value', passengerTypeOptions.value);
};
const getDescription = (value: string): string => {
  console.log('value', value);
  const item = passengerTypeOptions.value.find(item => item.code == value);
  return item ? item.description : '';
};

const add = (): void => {
  console.log('add');
  dialog.value = true;
};
const edit = async (record: any): Promise<void> => {
  console.log('record', record);
  const { data } = await passengerApi.queryPassengerDetail(record.id);
  console.log('data', data);
  formState.value = data;
  nextTick(() => {
    dialog.value = true;
  });
};
const deletePassenger = async (record: any): Promise<void> => {
  await passengerApi.deletePassenger(record.id);
  message.success('删除成功');
  getList();
};
const handleOk = async (): Promise<void> => {
  await passengerApi.savePassenger(formState.value);
  message.success('保存成功');
  dialog.value = false;
  getList();
};
const cancel = (): void => {
  Object.keys(formState.value).forEach(key => {
    formState.value[key] = '';
  });
  console.log('cancel');
};
</script>

<style scoped lang="less">
.passenger {
  :global(.ant-modal .ant-modal-footer) {
    text-align: center;
  }
}
</style>
